package com.ljy.crawler.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljy.crawler.entity.JdItem;
import com.ljy.crawler.service.IJdItemService;
import com.ljy.crawler.service.impl.JdItemServiceImpl;
import com.ljy.crawler.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @program: WebCrawler
 * @description: 定时任务
 * @author: liangjiayy
 * @create: 2022-01-25 22:34
 **/
@Component
@Slf4j
public class ItemTask {

    @Autowired
    private HttpUtils httpUtils;

    @Autowired
    private IJdItemService service;

    private ObjectMapper objectMapper = new ObjectMapper();

    @Scheduled(fixedDelay = 100 * 1000)//每隔100秒执行方法
    public void itemTask() throws Exception {
        log.info("{}下载...", new Date());
        String urlPre = "https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&wq=%E6%89%8B%E6%9C%BA&pvid=68345e498a29499890daff0573ff1fb5&s=56&click=0" +
                "&page=";
        String html;
        for (int i = 1; i < 5; i += 2) {
            html = httpUtils.getHtml(urlPre + i);
            parseHtml(html);
        }

    }

    private void parseHtml(String html) {
        // 获取spu
        Document document = Jsoup.parse(html);
        Elements elements = document.select("div#J_goodsList > ul > li");
        for (Element element : elements) {
            // spu可能为空串，表示只有一个款式，那么，就取sku作为spu的值
            String spu_str = element.attr("data-spu");
            Long spu = spu_str.length() == 0 ? Long.parseLong(element.attr("data-sku")) : Long.parseLong(spu_str);


            //获取sku
            Elements skuElements = element.select("li.ps-item");
            for (Element skuElement : skuElements) {
                long sku = Long.parseLong(skuElement.select("[data-sku]").attr("data-sku"));

                // 查数据库当前sku是否已经存在
                List<JdItem> items = service.list(new QueryWrapper<JdItem>().eq("sku", sku));

                if (items.size() > 0) {
                    log.info("sku为{},已经存在", sku);
                    continue;
                }

                // 获取详情
                JdItem item = new JdItem();
                //创建、更新时间
                item.setCreated(new Date());
                item.setUpdated(new Date());

                item.setUrl(getItemUrl(sku));

                item.setSku(sku);

                item.setSpu(spu);

                String imgUrl = skuElement.select("img[data-lazy-img]").attr("data-lazy-img");
                item.setPic(httpUtils.getImg(imgUrl));

                // 解析价格
                String price_url = "https://p.3.cn/prices/mgets?skuIds=J_" + sku;
                String price_json = httpUtils.getHtml(price_url);
                try {
                    double price = objectMapper.readTree(price_json).get(0).get("p").asDouble();
                    item.setPrice(price);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }


                // 解析标题
                Document parse = Jsoup.parse(httpUtils.getHtml(item.getUrl()));
                String title = parse.select("div.sku-name").text();
                item.setTitle(title);

                service.sava(item);

                log.info("已保存：{}", item);


            }

        }


    }

    private String getItemUrl(Long sku) {
        return "https://item.jd.com/" + sku + ".html";
    }
}